package com.ruoyi.prod.util;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.ruoyi.prod.entity.po.BaseEntity;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * mybatis plus 代码自动生成
 *
 * @author BaiBan
 * @since 2022/9/20
 */
@Slf4j
public class Generator {

    public final static String url = "jdbc:mysql://localhost:3306/asset?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    public final static String username = "root";
    public final static String password = "root";

    public static void main(String[] args) {

        // 新建路径
        String dirPath = System.getProperty("user.dir") + "\\ruoyi-admin\\src\\main\\java";
        String xmlPath = System.getProperty("user.dir") + "\\ruoyi-admin\\src\\main\\resources\\mapper\\prod";
        FastAutoGenerator.create(url, username, password)
                // 全局配置
                .globalConfig(builder -> {
                    builder.author("强子初") // 设置作者
                            .enableSwagger() // 开启 swagger模式
                            .outputDir(dirPath); // 指定输出目录
                })

                // 包配置
                .packageConfig(builder -> builder.parent("com.ruoyi.prod") // 设置父包名
                        .pathInfo(Collections.singletonMap(OutputFile.xml, xmlPath)) // 设置mapperXml生成路径
                        .entity("entity.po"))


                // 策略配置
                .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                        // controller配置
                        .controllerBuilder().enableRestStyle().enableHyphenStyle()

                        // service配置
                        .serviceBuilder().formatServiceFileName("%sService")

                        // mapper配置
                        .mapperBuilder().enableBaseResultMap().formatMapperFileName("%sMapper")

                        // entity配置
                        .entityBuilder()
                        .superClass(BaseEntity.class)
                        .enableTableFieldAnnotation()
                        .enableLombok()
                        .enableRemoveIsPrefix()
                        .addIgnoreColumns(getIgnoreColumnList())
                        .idType(IdType.ASSIGN_UUID)
                        .formatFileName("%s")
                        .build())
                .execute();
    }

    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

    private static List<String> getIgnoreColumnList() {
        List<String> list = new LinkedList<>();
        list.add("create_id");
        list.add("create_by");
        list.add("create_time");
        list.add("create_dept_id");
        list.add("create_dept");

        list.add("update_id");
        list.add("update_by");
        list.add("update_time");

        list.add("del_flag");
        return list;
    }
}
